// pages/me/offerPrice/offerPrice.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        searchParams:{
            pageNum: 1,
            pageSize: 6,
            queryType:'1',
            pages:'',
            total:'',
        },
        noData: true,
        InitData:false,
        offerPriceList:{},

    },

    doNothing(){},
    onLoad: function (options) {
        this.InitUserStatus();
        this.setData({
          'InitData': true
        })
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        let userInfo = app.globalData.userInfo;
        // 这里再验证一遍权限。
        if (userInfo.roleList.indexOf('viewCar') == -1) {
            app.showNoAuthOrNoGrant('您无权操作查看此页面');
            return;
        }else{
            this.goQuery();
        }
    },
    goQuery(){
      this.setData({
        'searchParams.pageNum':1,
        'searchParams.total':'',
        'searchParams.pages':'',
        'offerPriceList': {}
      });
      this.carManageOfferPriceList(this.data.searchParams);
    },
    carManageOfferPriceList(searchParams){
      // 整理完成进行搜索操作。
      app.sHttp.carManageOfferPriceList(searchParams).then(res=>{
          let offerPriceList = this.data.offerPriceList;
          offerPriceList[searchParams.pageNum] = res.result.offerPriceList;
          if (0 == res.result.total) {
            this.setData({
              'noData': true
            })
          }else{
              this.setData({
                  'noData': false,
                  'offerPriceList': offerPriceList,
                  'searchParams.total':res.result.total,
                  'searchParams.pages': res.result.pages
                });
          }
        },err=>{
          app.error(err.msg);
        })
    },
    switchType(e){
        let type = e.currentTarget.dataset.type;
        let _this = this;
        this.setData({
            'searchParams.queryType': type
        },(res)=>{
            _this.goQuery();
        })
    },
    onReachBottom(){
        if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
            //app.alert('没有更多了哦~');
        }else{
            let self = this;
            this.setData({
                'searchParams.pageNum': self.data.searchParams.pageNum+1
            });
            this.carManageOfferPriceList(this.data.searchParams);
        }
    },
    makePhoneCallByCheshang(e){
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.usermobile,
            success:function(res){
                console.log(res);
            },
            fail:function(){}
        })
    },
    makePhoneCall(e){
        let page = e.currentTarget.dataset.page;
        let index = e.currentTarget.dataset.idx;
        let item = this.data.offerPriceList[page][index];
        if(0 == item.status){
            app.sHttp.carManageOfferPriceHandle({carId: item.carId,detailId:item.detailId}).then(res=>{
                // 重新加载当前页面
                this.carManageOfferPriceList({
                    pageNum: page,
                    pageSize: 6,
                    queryType:this.data.searchParams.queryType});
                wx.makePhoneCall({
                    phoneNumber: item.userMobile,
                    success:function(res){
                        console.log(res);
                    },
                    fail:function(){}
                });
            },err=>{
                app.error(err.msg);
            })
        }else{
            wx.makePhoneCall({
                phoneNumber: item.userMobile,
                success:function(res){
                    console.log(res);
                },
                fail:function(){}
            })
        }
    },
    seeCarMsg(e){
        let cid = e.currentTarget.dataset.cid;
        wx.navigateTo({
          url: '/pages/card/card?cid=' + cid,
        })
    },
})